package com.yimu.lib.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextUtils {
	private static final String EMPTY_STRING = "";
	private static final String[] EMPTY_STRING_ARRAY = new String[0];
	private static final int[] EMPTY_INT_ARRAY = new int[0];
	private static final String DEFAULT_DELIMITER = ",";
	private static final String DEFAULT_DELIMITER_LINE = "-";

	/**
	 * 是否为空
	 * @param str 字符串
	 * @return true/false
	 */
	public static boolean isEmpty(String str) {
		return str == null || str.equals("");
	}

	/**
	 * 比较两个字符串
	 * @param a 字符串a
	 * @param b 字符串b
	 * @return ture为一样
	 */
	public static boolean equals(CharSequence a, CharSequence b) {
		if (a == b)
			return true;
		int length;
		if (a != null && b != null && (length = a.length()) == b.length()) {
			if (a instanceof String && b instanceof String) {
				return a.equals(b);
			} else {
				for (int i = 0; i < length; i++) {
					if (a.charAt(i) != b.charAt(i))
						return false;
				}
				return true;
			}
		}
		return false;
	}

	/**
	 * 获取字符串长度,一个汉字的长度为2
	 *
	 * @param str 字符串
	 * @return  字符串长度
	 */
	public static int getStrlen(String str) {
		int k = 0x80;
		char[] c = str.toCharArray();
		int len = 0;
		for (int i = 0; i < c.length; i++) {
			len++;
			if (!(c[i] / k == 0 ? true : false)) {
				len++;
			}
		}
		return len;
	}
	/**
	 * 全角转半角
	 *
	 * @param input 全角字符串
	 * @return 字符串转半角
	 */
	public static String ToSBC(String input) {
		char c[] = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == 12288) {
				c[i] = (char) 32;
				continue;
			}
			if (c[i] > 65280 && c[i] < 65375)
				c[i] = (char) (c[i] - 65248);
		}
		return new String(c);
	}
	/**
	 * 去空格
	 * @param str 去空格字符串
	 * @return 去空格
	 */
	public static String replaceBlank(String str) {
		String dest = "";
		if (str != null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}
		return dest;
	}
	public static String join(int[] tokens) {
		return join(DEFAULT_DELIMITER, tokens);
	}

	public static String join(long[] tokens) {
		return join(DEFAULT_DELIMITER, tokens);
	}

	public static String join(Object[] tokens) {
		return join(DEFAULT_DELIMITER, tokens);
	}

	public static CharSequence join(Iterable<CharSequence> list) {
		return join(DEFAULT_DELIMITER, list);
	}

	public static String join(CharSequence delimiter, int[] tokens) {
		if (tokens == null || tokens.length == 0) {
			return EMPTY_STRING;
		}
		if (tokens.length == 1) {
			return String.valueOf(tokens[0]);
		}
		StringBuilder sb = new StringBuilder();
		boolean firstTime = true;
		for (int token : tokens) {
			if (firstTime) {
				firstTime = false;
			} else {
				sb.append(delimiter);
			}
			sb.append(token);
		}
		return sb.toString();
	}

	public static String join(CharSequence delimiter, long[] tokens) {
		if (tokens == null || tokens.length == 0) {
			return EMPTY_STRING;
		}
		if (tokens.length == 1) {
			return String.valueOf(tokens[0]);
		}
		StringBuilder sb = new StringBuilder();
		boolean firstTime = true;
		for (long token : tokens) {
			if (firstTime) {
				firstTime = false;
			} else {
				sb.append(delimiter);
			}
			sb.append(token);
		}
		return sb.toString();
	}

	public static String join(CharSequence delimiter, Object[] tokens) {
		if (tokens == null || tokens.length == 0) {
			return EMPTY_STRING;
		}
		if (tokens.length == 1) {
			return String.valueOf(tokens[0]);
		}
		StringBuilder sb = new StringBuilder();
		boolean firstTime = true;
		for (Object token : tokens) {
			if (firstTime) {
				firstTime = false;
			} else {
				sb.append(delimiter);
			}
			sb.append(token);
		}
		return sb.toString();
	}

	public static String join(CharSequence delimiter, Iterable tokens) {
		if (tokens == null) {
			return EMPTY_STRING;
		}
		StringBuilder sb = new StringBuilder();
		boolean firstTime = true;
		for (Object token : tokens) {
			if (firstTime) {
				firstTime = false;
			} else {
				sb.append(delimiter);
			}
			sb.append(token);
		}
		return sb.toString();
	}

	public static String[] split(String text) {
		return split(text, DEFAULT_DELIMITER);
	}

	public static String[] split(String text, String expression) {
		if (isEmpty(text)) {
			return EMPTY_STRING_ARRAY;
		} else {
			return text.split(expression, -1);
		}
	}

	public static String[] split(String text, Pattern pattern) {
		if (isEmpty(text)) {
			return EMPTY_STRING_ARRAY;
		} else {
			return pattern.split(text, -1);
		}
	}

	public static int[] splitAsInt(String text) {
		return splitAsInt(text, DEFAULT_DELIMITER);
	}

	public static int[] splitAsInt(String text, String expression) {
		return toIntArray(split(text, expression));
	}

	public static int[] splitAsInt(String text, Pattern pattern) {
		return toIntArray(split(text, pattern));
	}

	public static int[] toIntArray(String[] array) {
		if (array == null) {
			return EMPTY_INT_ARRAY;
		}
		int len = array.length;
		if (len == 0) {
			return EMPTY_INT_ARRAY;
		}
		int[] result = new int[len];
		for (int i = 0; i < len; i++)
			result[i] = Integer.parseInt(array[i]);
		return result;
	}

	public static List<String> long2str(long... values) {
		List<String> result = new ArrayList<>();
		for (long v : values) {
			result.add(String.valueOf(v));
		}
		return result;
	}


	public static String[] split2(String text) {
		return split(text, DEFAULT_DELIMITER_LINE);
	}
}
